import $get from 'lodash/get'

const fname = '[h5-request-response]'
const isProd = process.env.NODE_ENV === 'production'

// 统一处理服务端返回信息与上报数据
// 处理数据和日志: fixAfter: resolveServerFixAfter
// 仅处理日志: resolveServerFixAfter.call(this, res)
function resolveServerFixAfter (res) {
	const logger = this.logger
	if (logger) {
		if (typeof logger.info === 'function') {
			if (isProd) {
				let json = JSON.stringify(res)
				logger.info(`${fname} ${json}`)
			} else {
				// 服务日志也可以用cookie开关了
				let cookie = $get(this, 'ctx.request.header.cookie', '')
				if (cookie.indexOf('debug_felog=on') > -1) {
					console.info(fname, res)
				}
			}
		}
	}
}

export default resolveServerFixAfter
